/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-6-3 下午3:55 Created by lyyang
 */
package com.xunton.bss.weixin.api;

import java.net.URI;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;

import com.xunton.bss.weixin.api.bonus.request.DeliverBonusRequest;

/**
 * 工具类
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-6-3 下午3:55
 */
public class Utils
{
    public static boolean checkSignature(String token, String timestamp, String nonce, String signature)
            throws Exception
    {
        String[] params = new String[3];
        params[0] = token;
        params[1] = timestamp;
        params[2] = nonce;

        Arrays.sort(params);
        String key = params[0] + params[1] + params[2];
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(key.getBytes());
        String tmp = bytes2Hex(md.digest());
        return signature.equalsIgnoreCase(tmp);

    }

    public static String createSignature(String... params)
            throws Exception
    {
        Arrays.sort(params);
        String key = "";
        for (String p : params) {
            key += p;
        }
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(key.getBytes());
        return bytes2Hex(md.digest());


    }

    public static String bytes2Hex(byte[] bts)
    {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }

    public static String md5Signature(Map params, String apiKey)
            throws Exception
    {
        String[] keys = (String[])params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        String tmp = "";
        for (String key : keys) {
            Object o = params.get(key);
            if (o!=null){

                    tmp += "&" + key + "=" + o;
            }
        }
        tmp = tmp.substring(1) + "&key=" + apiKey;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(tmp.getBytes("UTF-8"));
        String result = Utils.bytes2Hex(md.digest()).toUpperCase();
        return result;
    }

    public static void main(String[] args)
    {
        try {
//            DeliverBonusRequest bonusRequest = new DeliverBonusRequest();
//        bonusRequest.setActName("xunton");
//        bonusRequest.setClientIp("121.40.87.47");
//        bonusRequest.setMaxValue(110);
//        bonusRequest.setMchId("1241808602");
//        bonusRequest.setMinValue(100);
//        bonusRequest.setNickName("xunton");
//        bonusRequest.setRemark("remark");
//        bonusRequest.setReOpenId("oCi76t5vtZttO57dyX7q1P3_5S6Q");
//        bonusRequest.setSendName("xunton");
//        bonusRequest.setTotalAmount(200);
//        bonusRequest.setWishing("wishing");
//        bonusRequest.setWxAppId("wx6ed3ee6bdcd70fa0");
//        bonusRequest.calculateSign("aDdfoi123D093Ilfi900azcjholoaa2Z");
//
//            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd0HHmmssSSS");
//            System.out.println(UUID.randomUUID().toString().replaceAll("-",""));
//
//            System.out.println(createSignature("23456", "141231233", "345667", "456789"));
            HashMap map = new HashMap();
            map.put("appid",1);
            map.put("remark","测试");
            System.out.println(md5Signature(map,"7"));

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
